using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using System.Windows;
using System.Drawing.Imaging;
using System.Drawing;

namespace InsertVideo
{
    public class GenerateHTML
    {
        ISmartContent _content;
        VideoSettings _settings;
        string embed;
        string code;
        string id;
        string provider;
        decimal ratio;
        int width;
        int height;

        public GenerateHTML(ISmartContent content)
        {
            _content = content;
            string transparancy = "";
            string autoplay = "";
            string youtuberel = "";

            // Get the settings from the SmartContent
            _settings = new VideoSettings(_content.Properties);

            // Get the initial embed code
            embed = _settings.Embed;

            // Get the Video source provider
            provider = _settings.Provider;

            // Get the video ID
            id = _settings.Id;

            // Get the aspect ratio for the video
            ratio = _settings.Ratio;

            // Set the width
            width = _settings.Width;

            if (_settings.Transparency)
                transparancy = " wmode=\"transparent\"";
            if (_settings.Autoplay)
                autoplay = _settings.AutoPlayValue;
            if (!_settings.YouTubeRelative && _settings.Provider == "youtube")
                youtuberel = "&rel=0";

            // Set the height using the width and aspect ratio
            height = (int)(width / ratio); 

            // Set the final embed code
            code = string.Format(embed, width.ToString(), height.ToString(), id, transparancy, autoplay, youtuberel);

            _settings.FinalEmbedCode = code;         
            
        }

        public void GenerateHTMLCode()
        {
            try
            {
                HtmlScreenCapture html = new HtmlScreenCapture(code, width);
                html.HtmlScreenCaptureAvailable += new HtmlScreenCaptureAvailableHandler(html_HtmlScreenCaptureAvailable);
                Image image = html.CaptureHtml(10000);
                _content.Files.AddImage("videocapture-" + id + ".png", image, ImageFormat.Png);

                // Store the final embedded code in the SmartContent Properties
                _settings.FinalEmbedCode = code;         
            }
            catch
            {
                MessageBox.Show("Video Plugin could not insert your video at this time, please check that you are connected to the internet and that you have available bandwidth to watch the video.\nTrying to insert again might be an idea.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        void html_HtmlScreenCaptureAvailable(object sender, HtmlScreenCaptureAvailableEventArgs e)
        {

            // get the bitmap
            Bitmap bitmap = e.Bitmap;

            // count the number of black pixels
            Color backgroundColor = Color.FromArgb(255, 0, 0, 0);
            int blackPixels = 0;
            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    Color pixelColor = bitmap.GetPixel(x, y);

                    if (pixelColor == backgroundColor)
                        blackPixels++;
                }
            }

            // if background pixels make up more than 80% of all pixels then
            // this is not a thumbnail
            int totalPixels = bitmap.Width * bitmap.Height;
            double pctBlack = Convert.ToDouble(blackPixels) / Convert.ToDouble(totalPixels);

            if (provider == "brightcove")
            {
                e.CaptureCompleted = pctBlack < .15F;
            }
            if (provider == "google" || provider == "youtube")
            {
                e.CaptureCompleted = pctBlack < .80F;
            }
            else if (provider == "soapbox" || provider == "myspace" || provider == "myvideo" || provider == "muveemix")
            {
                e.CaptureCompleted = pctBlack < .70F;
            }
            else if (provider == "yahoo")
            {
                e.CaptureCompleted = true;
            }
            else if (provider == "divshare")
            {
                e.CaptureCompleted = pctBlack > .60F;
            }
        }



    }
}
